package iws.g5.uebung2.aufgabe3;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;

/**
 * Game of Life 
 *
 * Copyright 2013 Kristian Kraljic
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author <a href=mailto:kristian@kra.lc>Kristian Kraljic</a>
 */
public class GameOfLifeFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	public final GameOfLife game;
	
	public static void main(String[] args) {
		new GameOfLifeFrame().setVisible(true);
	}
	
	public GameOfLifeFrame() {
		super("GameOfLife (Idea: John Horton Conway, Implementation: Kristian Kraljic)");
		this.game = new GameOfLife();
		game.extinguish();
		
		((JPanel)this.getContentPane()).setBorder(new EmptyBorder(10,10,10,10));
		this.setLayout(new BorderLayout(10,10));
		this.add(game,BorderLayout.CENTER);
		
		JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
		this.add(controls,BorderLayout.SOUTH);
		
		JButton extinguish = new JButton("Extinguish");
		controls.add(extinguish);
		extinguish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				game.extinguish();
			}
		});
		JButton next = new JButton("Next Generation");
		controls.add(next);
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				game.compute();
			}
		});
		final JToggleButton simulate = new JToggleButton("Simulate");
		controls.add(simulate);
		simulate.addActionListener(new ActionListener() {
			private Thread simulation;
			@Override public void actionPerformed(ActionEvent arg0) {
				if(simulate.isSelected())
					(simulation = new Thread() {
						@Override public void run() {
							try {
								while(true) {
									game.compute();
									Thread.sleep(100);
								}
							} catch(InterruptedException e) {}
						}
					}).start();
				else simulation.interrupt();
			}
		});
		final JLabel status = new JLabel();
		controls.add(status);
		game.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent event) {
				status.setText("Generation: "+game.generation()+", Alive: "+game.countAlive());
			}
		});
		game.getActionListeners()[0].actionPerformed(null);
		
		this.setLocationByPlatform(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.pack();
	}
}
